import { importExcel, exportExcel } from '@/utils/jsonToExcel'

export default {
  methods: {
    exportClick() {
      const arr = []
      const info1 = [
        this.$t('SEGMENT_NAME') + '：' + this.formData.segmentName,
        '',
        '',
        '',
        this.$t('CABLE_NAME') +
          '：' +
          (this.formData.cable ? this.formData.cable.cableName : '--'),
        '',
        '',
        ''
      ]
      const info2 = [
        this.$t('A_END_STATION_NAME') +
          '：' +
          this.getRegionName(this.formData.startStationId),
        '',
        '',
        '',
        this.$t('Z_END_STATION_NAME') +
          '：' +
          this.getRegionName(this.formData.endStationId),
        '',
        '',
        ''
      ]
      const info3 = [
        this.$t('ANODE') + '：' + this.formData.startNode.nodeName,
        '',
        '',
        '',
        this.$t('ZNODE') + '：' + this.formData.endNode.nodeName,
        '',
        '',
        ''
      ]
      arr.push(info1)
      arr.push(info2)
      arr.push(info3)
      const title1 = [
        this.$t('SEQUENCE'),
        this.$t('CHANNEL_STATUS'),
        this.$t('LINK_LOSS'),
        this.formData.startNode.nodeName,
        this.formData.startNode.nodeName,
        this.formData.endNode.nodeName,
        this.formData.endNode.nodeName
      ]
      const title2 = [
        this.$t('SEQUENCE'),
        this.$t('CHANNEL_STATUS'),
        this.$t('LINK_LOSS'),
        this.$t('A_EEND_PORT'),
        this.$t('Z_EEND_PORT'),
        this.$t('A_EEND_PORT'),
        this.$t('Z_EEND_PORT')
      ]
      // s 意为 start ，即开始的单元格
      // r 是 row ，表示行号，从 0 计起
      // c 是 col ，表示列号，从 0 计起
      const merge = [
        { s: { r: 0, c: 0 }, e: { r: 0, c: 3 }},
        { s: { r: 0, c: 4 }, e: { r: 0, c: 7 }},
        { s: { r: 1, c: 0 }, e: { r: 1, c: 3 }},
        { s: { r: 1, c: 4 }, e: { r: 1, c: 7 }},
        { s: { r: 2, c: 0 }, e: { r: 2, c: 3 }},
        { s: { r: 2, c: 4 }, e: { r: 2, c: 7 }},
        { s: { r: 3, c: 0 }, e: { r: 4, c: 0 }},
        { s: { r: 3, c: 1 }, e: { r: 4, c: 1 }},
        { s: { r: 3, c: 2 }, e: { r: 4, c: 2 }},
        { s: { r: 3, c: 3 }, e: { r: 3, c: 4 }}
      ]
      if (this.formData.cableId) {
        title1.splice(5, 0, this.formData.cable.cableName)
        title2.splice(5, 0, this.$t('FIBER_CORE'))
        merge.push({ s: { r: 3, c: 5 }, e: { r: 3, c: 5 }})
        merge.push({ s: { r: 3, c: 6 }, e: { r: 3, c: 7 }})
      } else {
        merge.push({ s: { r: 3, c: 5 }, e: { r: 3, c: 6 }})
      }
      arr.push(title1)
      arr.push(title2)
      this.oldChannelList.forEach((i, index) => {
        const channelItem = [
          index + 1,
          this.dictMap.CHANNEL_STATUS[i.status],
          i.linkLoss > 990000
            ? '--'
            : this.changeDecimal(i.linkLoss, 3),
          this.getPortName(this.aAllPortList, i.startPortId),
          i.startPort.bizName,
          i.endPort.bizName,
          this.getPortName(this.zAllPortList, i.endPortId)
        ]
        if (this.formData.cableId) {
          channelItem.splice(5, 0, i.fiber.fiberName)
        }
        arr.push(channelItem)
      })
      this.exportFunc(arr, this.$t('SEGMENT_INFO'), merge)
    },
    templateExportClick() {
      const exampleData = {
        AFS: 'A-1、B-1、1',
        ODF: 'P1-1-输入、P1-1-输出',
        TRANSPORT: '子架1-P1-1发、子架1-P1-1收、子架1-P1-1',
        LIGHT_SPLIT: '输入1'
      }
      const arr = []
      const title = [
        this.formData.startNode.nodeName,
        this.formData.endNode.nodeName
      ]
      console.log('example', this.formData)

      const example = [
        this.$t('A_EXAMPLE', { name: exampleData[this.formData.startNodeCategory] }),
        this.$t('Z_EXAMPLE', { name: exampleData[this.formData.endNodeCategory] })
      ]
      if (this.formData.cableId) {
        title.splice(1, 0, this.formData.cable.cableName)
        example.splice(1, 0, this.$t('FIBER_EXAMPLE')
        )
      }
      console.log('example', example)
      arr.push(title)
      arr.push(example)
      this.exportFunc(arr, this.$t('IMPORT_TPL'))
    },
    exportFunc(arr, fileName, merge = null) {
      console.log('arr', arr)
      const sheets = []
      const sheetNames = []
      sheets.push(arr)
      sheetNames.push(`${this.$t('SEGMENT_INFO')}`)
      exportExcel(
        sheets,
        `${this.formData.segmentName}${fileName}`,
        sheetNames,
        'array',
        merge
      )
      this.addLog('export') // 记录导出日志
    },
    importClick(file) {
      importExcel(file).then((res) => {
        console.log('导入成功')
        console.log('res', res)
        this.importIng = true
        const startKey = this.formData.startNode.nodeName
        const endKey = this.formData.endNode.nodeName
        const aList = []
        const zList = []
        const fiberList = []
        const newList = []
        let fiberKey = ''
        res.data.shift() // 删除示例
        res.data.forEach((i) => {
          const canPush = [true, true, true]
          const item = { ...this.channelItem }
          const startPort = this.aPortList.find(
            (j) => j.bizName === i[startKey].toString()
          )
          if (
            startPort &&
            !this.oldChannelList.some(
              (j) => j.startPort.bizName === i[startKey] && j.channelId
            ) &&
            !newList.some(
              (j) => j.startPort && j.startPort.bizName === i[startKey]
            )
          ) {
            item.startPortId = startPort.portId
            item.startPort = startPort
          } else {
            canPush[0] = false
            i[startKey] && aList.push(i[startKey])
          }
          const endPort = this.zPortList.find(
            (j) => j.bizName === i[endKey].toString()
          )
          if (
            endPort &&
            !this.oldChannelList.some(
              (j) => j.endPort.bizName === i[endKey] && j.channelId
            ) &&
            !newList.some((j) => j.endPort && j.endPort.bizName === i[endKey])
          ) {
            item.endPortId = endPort.portId
            item.endPort = endPort
          } else {
            canPush[2] = false
            i[endKey] && zList.push(i[endKey])
          }
          if (this.formData.cableId) {
            fiberKey = this.formData.cable.cableName
            const fiber = this.cableFiberList.find(
              (j) => j.fiberName === i[fiberKey].toString()
            )
            if (
              fiber &&
              !this.oldChannelList.some(
                (j) => j.fiber.fiberName === i[fiberKey] && j.channelId
              ) &&
              !newList.some((j) => j.fiber && j.fiber.fiberName === i[fiberKey])
            ) {
              item.fiberId = fiber.fiberId
              item.fiber = fiber
            } else {
              canPush[1] = false
              i[fiberKey] && fiberList.push(i[fiberKey])
            }
          }
          !canPush.some((i) => !i) && newList.push(item)
        })
        const errmsg = []
        if (aList.length) {
          errmsg.push(
            this.$t('ANODE') +
              startKey +
              this.$t('PORT') +
              this.$t('NOT_EXIST_OR_OCCUPIED', {
                msg: aList.join()
              })
          )
        }
        if (zList.length) {
          errmsg.push(
            this.$t('ZNODE') +
              endKey +
              this.$t('NOT_EXIST_OR_OCCUPIED', {
                msg: zList.join()
              })
          )
        }
        if (fiberList.length) {
          errmsg.push(
            this.$t('CABLE_NAME') +
              fiberKey +
              this.$t('FIBER_CORE') +
              this.$t('NOT_EXIST_OR_OCCUPIED', {
                msg: fiberList.join()
              })
          )
        }
        console.log('数据处理完成，开始渲染')
        if (errmsg.length) {
          this.$message.error(this.$t('IMPORT_FAILD') + '：' + errmsg.join('；'))
        } else {
          this.importVisible = false
          this.$message.success(
            this.$t('IMPORT_SUCCEED', { msg: newList.length })
          )
          if (newList.length > 0) {
            this.formData.segmentChannelList = [
              ...this.oldChannelList,
              ...newList
            ]
          }
        }
        console.log('importIng', this.importIng)
        this.$nextTick(() => {
          this.importIng = false
          this.addLog('import') // 记录导入日志
        })
      })
    }
  }
}
